using Os.Infra;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;

namespace Os.Domain
{
	public class StatusOs
	{

		private int _idStatus;
		private string _nome;
		private bool _persisted;
		private bool _modified;

        private const string SPSELECTID = "SP_StatusSelId";
        private const string SPSELECT = "SP_StatusSel";
        private const string SPINSERT = "SP_StatusIns";
        private const string SPUPDATE = "SP_StatusUpd";
        private const string SPDELETE = "SP_StatusDel";

		public int IdStatus
		{
			get
			{
				return this._idStatus;
			}
			set
			{
				this._idStatus = value;
				this._modified = true;
			}
		}
		public string Nome
		{
			get
			{
				return this._nome;
			}
			set
			{
				this._nome = value;
				this._modified = true;
			}
		}
		public StatusOs()
		{
		}
		public StatusOs(int idStatus)
		{
			this._idStatus = idStatus;
			this._persisted = true;
		}
		private List<MySqlParameter> ObterParametros()
		{
			return new List<MySqlParameter>
			{
				new MySqlParameter("?idStatus", MySqlDbType.Int32, 11), 
				new MySqlParameter("?nome", MySqlDbType.VarChar, 50)
			};
		}
		private void PreencherParametros(List<MySqlParameter> parms)
		{
			parms[0].Value = this._idStatus;
			parms[1].Value = this._nome;
			if (!this._persisted)
			{
				parms[0].Direction = ParameterDirection.Output;
			}
			else
			{
				parms[0].Direction = ParameterDirection.Input;
			}
		}
		private void Inserir()
		{
			List<MySqlParameter> parameters = this.ObterParametros();
			this.PreencherParametros(parameters);
			using (MySqlConnection mySqlConnection = new MySqlConnection(MysqlData.CONN_STRING))
			{
				mySqlConnection.Open();
				try
				{
					MySqlCommand mySqlCommand = MysqlData.ExecutarComando(CommandType.StoredProcedure, "SP_StatusIns", parameters);
					this._idStatus = Convert.ToInt32(mySqlCommand.Parameters["?id_Status"].Value);
					mySqlCommand.Parameters.Clear();
					this._persisted = true;
					this._modified = false;
				}
				catch (Exception ex)
				{
					throw ex;
				}
			}
		}
		private void Inserir(MySqlTransaction trans)
		{
			List<MySqlParameter> parameters = this.ObterParametros();
			this.PreencherParametros(parameters);
			MySqlCommand mySqlCommand = MysqlData.ExecutarComando(trans, CommandType.StoredProcedure, "SP_StatusIns", parameters);
			this._idStatus = Convert.ToInt32(mySqlCommand.Parameters["?parIdStatus"].Value);
			mySqlCommand.Parameters.Clear();
			this._persisted = true;
			this._modified = false;
		}
		private void Atualizar()
		{
			if (this._modified)
			{
				List<MySqlParameter> parameters = this.ObterParametros();
				this.PreencherParametros(parameters);
				MysqlData.ExecutarComando(CommandType.StoredProcedure, "SP_StatusUpd", parameters);
				this._modified = false;
			}
		}
		private void Atualizar(MySqlTransaction trans)
		{
			if (this._modified)
			{
				List<MySqlParameter> parameters = this.ObterParametros();
				this.PreencherParametros(parameters);
				MysqlData.ExecutarComando(trans, CommandType.StoredProcedure, "SP_StatusUpd", parameters);
				this._modified = false;
			}
		}
		public static void Remover(int idStatus)
		{
			List<MySqlParameter> parms = new List<MySqlParameter>
			{
				new MySqlParameter("?parIdStatus", MySqlDbType.Int32, 11)
			};
			parms[0].Value = idStatus;
			MysqlData.ExecutarComando(CommandType.StoredProcedure, "SP_StatusDel", parms);
		}
		public static void Remover(MySqlTransaction trans, int idStatus)
		{
			List<MySqlParameter> parms = new List<MySqlParameter>
			{
				new MySqlParameter("?parIdStatus", MySqlDbType.Int32, 11)
			};
			parms[0].Value = idStatus;
			MySqlCommand mySqlCommand = MysqlData.ExecutarComando(trans, CommandType.StoredProcedure, "SP_StatusDel", parms);
			mySqlCommand.Parameters.Clear();
		}
		public void Salvar()
		{
			if (!this._persisted)
			{
				this.Inserir();
			}
			else
			{
				this.Atualizar();
			}
		}
		public void Salvar(MySqlTransaction trans)
		{
			if (!this._persisted)
			{
				this.Inserir(trans);
			}
			else
			{
				this.Atualizar(trans);
			}
		}
		private static void RealizarPersistencia(IDataReader dado, StatusOs objStatus)
		{
			try
			{
                if (dado["idStatus"] != DBNull.Value)
				    objStatus._idStatus = Convert.ToInt32(dado["idStatus"]);
                if (dado["nome"] != DBNull.Value)
				    objStatus._nome = Convert.ToString(dado["nome"]);

				objStatus._persisted = true;
				objStatus._modified = false;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public static StatusOs CarregarObjeto(int idStatus)
		{
			StatusOs status = null;
			using (IDataReader dado = StatusOs.CarregarDadosBanco(idStatus))
			{
				if (dado.Read())
				{
					status = new StatusOs();
					StatusOs.RealizarPersistencia(dado, status);
				}
                dado.Close();
                dado.Dispose();
			}
			return status;
		}
		public static List<StatusOs> CarregarTodos()
		{
			List<StatusOs> allStatus = new List<StatusOs>();
			using (IDataReader dado = StatusOs.CarregarDadosBanco())
			{
				while (dado.Read())
				{
					StatusOs statusOs = new StatusOs();
					StatusOs.RealizarPersistencia(dado, statusOs);
                    allStatus.Add(statusOs);
				}
                dado.Close();
                dado.Dispose();
			}
            return allStatus;
		}
		public void CompletarObjeto()
		{
			using (IDataReader dado = StatusOs.CarregarDadosBanco(this._idStatus))
			{
				if (dado.Read())
				{
					StatusOs.RealizarPersistencia(dado, this);
				}
                dado.Close();
                dado.Dispose();
			}
		}
		private static IDataReader CarregarDadosBanco(int idStatus)
		{
			List<MySqlParameter> parms = new List<MySqlParameter>
			{
				new MySqlParameter("?idStatus", MySqlDbType.Int32, 11)
			};
			parms[0].Value = idStatus;
			return MysqlData.ExecutarLeitura(CommandType.StoredProcedure, "SP_StatusSelId", parms);
		}
		private static IDataReader CarregarDadosBanco()
		{
			return MysqlData.ExecutarLeitura(CommandType.StoredProcedure, "SP_StatusSel", null);
		}
	}
}
